package com.ys.controller.api.base;

import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ys.config.MenuId;
import com.ys.entity.base.TbSite;
import com.ys.entity.sys.YsCol;
import com.ys.enume.sys.YsColEnum;
import com.ys.service.base.TbSiteService;
import com.ys.service.sys.CommonApiService;
import com.ys.service.sys.UploadService.UploadDto;
import com.ys.util.ExportExcelUtils;
import com.ys.vo.LayuiTableVoJA;
import com.ys.vo.R;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/api/inter/tbSite")
public class ApiInterTbSiteController {
	
	@Autowired
	private CommonApiService<TbSite> commonApiService;
	@Autowired
	private TbSiteService tbSiteService;
	
	@RequiresPermissions("tbSite:view")
	@RequestMapping("/search")
    @ResponseBody
	public LayuiTableVoJA search(@ModelAttribute TbSite cond){
		
		cond = setSeachSql(cond);
		return commonApiService.searchLay(cond);
	}
	/**
	 * 新增
	 */
	@RequestMapping("/add")
	@ResponseBody
	@RequiresPermissions("tbSite:add")
	public R add(@RequestParam Map<String, Object> params) {
		try {
			//修改
			return tbSiteService.add(params);
		} catch (Exception e) {
			e.printStackTrace();
			return R.error("系统正在开小差，请联系客服，我们将第一时间为您解决！");
		}
	}

	/**
	 * 编辑
	 */
	@RequiresPermissions("tbSite:edit")
	@PostMapping("/edit")
    @ResponseBody
	public R edit(@RequestParam Map<String, Object> params){
		try {
			return tbSiteService.edit(params);
		} catch (Exception e) {
			e.printStackTrace();
			return R.error("系统正在开小差，请联系客服，我们将第一时间为您解决！");
		}
	}
	
	/*
	 * 导出
	 */
	@RequiresPermissions("tbSite:export")
	@RequestMapping("/export")
    @ResponseBody
    public R export(@ModelAttribute TbSite cond) throws Exception{
		
		cond = setSeachSql(cond);
		cond.setOrderby("A.Id DESC");
		
		cond.setPage(0).setSize(999999);

		List<Map<String, Object>> listData = cond.queryMap();
        
    	List<YsCol> listField = new YsCol()
    			.setMenuId(MenuId.TB_Site)
    			.setFrontShow(YsColEnum.FrontShow.SHOW.value)
    			.setOrderby("A.SortNo asc")
    			.queryList();
    	
    	
        ExportExcelUtils<TbSite> util = new ExportExcelUtils<TbSite>(new TbSite());
        
        String toFilePath = util.exportExcel(listData, listField, "站点信息维护");
        UploadDto dto = commonApiService.uploadLocalToServer(toFilePath);
        return R.ok(dto.getUrl());
    }

	/**
	 * 弹出框
	 */
	@RequestMapping("/searchForModal")
	@ResponseBody
	public LayuiTableVoJA searchForModal(@ModelAttribute TbSite cond){
		cond.setStatus(1);
    	
    	cond = setSeachSql(cond);
    	
		return commonApiService.searchLay(cond);
	}

	/**
	 * 查询站点列表
	 */
	@RequestMapping("/tbSiteList")
	@ResponseBody
	public R tbSiteList(HttpServletRequest request, @RequestParam("list") List<String> idList){
		try {
			StringBuffer sqlId = new StringBuffer();
			for (String id : idList){
				sqlId.append(id);
				sqlId.append(",");
			}
			List<Map<String,Object>> tbSiteList = new TbSite()
			.setStatus(1)
			.addLeftJoin("TP_AmazonRegion B ON B.Code = A.RegionCode")
					.addField("B.Name regionName")

			.addLeftJoin("TB_Platform B1 ON B1.Code = B.PlatformCode")
					.addField("B1.Code platformCode")
					.addField("B1.Name platformName")

			.addLeftJoin("TB_Country C ON C.Id = A.CountryId")
					.addField("C.Code countryCode")
					.addField("C.Name countryName")

			.addLeftJoin("TP_AmazonShopExchangeRate C1 ON C1.Id = C.AmazonShopExchangeRateId")
					.addField("C.Code currencyCode")
					.addField("C.Name currencyName")
			.where("A.id IN("+sqlId.substring(0,sqlId.length()-1) + ")")
			.queryMap();

			return R.ok(tbSiteList);
		} catch (Exception e) {
			return R.error("系统正在开小差，请联系客服，我们将第一时间为您解决！");
		}
	}

    /**
     * 设置查询的cond的sql设置
     */
    public TbSite setSeachSql(TbSite cond){
    	
    	cond.addLeftJoin("TP_AmazonRegion B ON B.Code = A.RegionCode")
    		.addField("B.Name regionName");
    	
    	cond.addLeftJoin("TB_Platform B1 ON B1.Code = B.PlatformCode")
    		.addField("B1.Code platformCode")
    		.addField("B1.Name platformName");
    	
    	cond.addLeftJoin("TB_Country C ON C.Id = A.CountryId")
    		.addField("C.Code countryCode")
    		.addField("C.Name countryName");
    	
    	cond.addLeftJoin("TP_AmazonShopExchangeRate C1 ON C1.Id = C.AmazonShopExchangeRateId")
    		.addField("C.Code currencyCode")
    		.addField("C.Name currencyName");
    	
        return  cond;
    }

}
